package com.liuanbang.saro.sarodemo.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.base.BaseActivity;
import com.liuanbang.saro.sarodemo.ui.items.statusmanager.OnRetryListener;
import com.liuanbang.saro.sarodemo.ui.items.statusmanager.OnShowHideViewListener;
import com.liuanbang.saro.sarodemo.ui.items.statusmanager.StatusLayoutManager;
import com.liuanbang.saro.sarodemo.util.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘安邦 on 2017/3/9.
 * Email:liuanbang@aliyun.com
 * 描述:加入通用Status 动态更换状态布局  2017/3/9
 * 修改:
 */
public class StatusLayoutActivity extends BaseActivity {
    protected StatusLayoutManager statusLayoutManager;
    @Bind(R.id.toolBar)
    Toolbar toolBar;
    @Bind(R.id.button1)
    Button button1;
    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.button3)
    Button button3;
    @Bind(R.id.button4)
    Button button4;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_status_layout);
        ButterKnife.bind(this);

        toolBar.setTitleTextColor(Color.WHITE);
        toolBar.setTitle("动态更换状态布局");
        toolBar.inflateMenu(R.menu.menu_status_layout_activity);
        toolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_contents:
                        ToastUtils.showToast("这是啥");
                        break;
                    case R.id.action_emptyData:
                        statusLayoutManager.showEmptyData();
                        break;
                    case R.id.action_error:
                        statusLayoutManager.showError();
                        break;
                    case R.id.action_loading:
                        statusLayoutManager.showLoading();
                        break;
                    case R.id.action_networkError:
                        statusLayoutManager.showNetWorkError();
                        break;
                    case R.id.action_neirong:
                        statusLayoutManager.showContent();
                        break;
                }
                return true;
            }
        });

       /* setSupportActionBar(toolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //该监听必须放在 setSupportActionBar(toolBar);之后
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtil.finishActivity();
            }
        });*/


        //   LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.id.main_rl);
        //获取跟布局
        ViewGroup mainLinearLayout = (ViewGroup) this.getWindow().getDecorView();
        statusLayoutManager = StatusLayoutManager.newBuilder(this)
                .contentView(R.layout.activity_content)
                .emptyDataView(R.layout.activity_emptydata)
                .errorView(R.layout.activity_error)
                .loadingView(R.layout.activity_loading)
                .netWorkErrorView(R.layout.activity_networkerror)
                .retryViewId(R.id.button_try)
                .onShowHideViewListener(new OnShowHideViewListener() {
                    @Override
                    public void onShowView(View view, int id) {
                    }

                    @Override
                    public void onHideView(View view, int id) {
                    }
                }).onRetryListener(new OnRetryListener() {
                    @Override
                    public void onRetry() {
                        statusLayoutManager.showLoading();

                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        statusLayoutManager.showContent();
                                        // statusLayoutManager.getRootLayout();
                                    }
                                });
                            }
                        }).start();

                    }
                }).build();

        mainLinearLayout.addView(statusLayoutManager.getRootLayout(), 1);

        statusLayoutManager.showLoading();
    }


    @Override
    public Toolbar getToolbar() {

        return null;
    }

    @Override
    protected void setListener() {
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                ToastUtils.showToast("1..");
                break;
            case R.id.button2:
                ToastUtils.showToast("2..");
                break;
            case R.id.button3:
                ToastUtils.showToast("3..");
                break;
            case R.id.button4:
                ToastUtils.showToast("4..");
                break;
        }
    }
}
